package com.beidouin.sensortest;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import com.friendlyarm.AndroidSDK.GPIOEnum;
import com.friendlyarm.AndroidSDK.HardwareControler;

public class KeyActivity extends AppCompatActivity implements Runnable{

//    TextView GPIOtxv1,GPIOtxv2,GPIOtxv3,GPIOtxv4;
    Button btn1,btn2,btn3,btn4;
    TextView tv1,tv2,tv3,tv4;
    int lastkeysta[];
    Handler handler;
    Thread t = new Thread(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key);

//        GPIOtxv1= (TextView) findViewById(R.id.GPIO9);
//        GPIOtxv2= (TextView) findViewById(R.id.GPIO10);
//        GPIOtxv3= (TextView) findViewById(R.id.GPIO11);
//        GPIOtxv4= (TextView) findViewById(R.id.GPIO12);

        btn1= (Button) findViewById(R.id.button1);
        btn2= (Button) findViewById(R.id.button2);
        btn3= (Button) findViewById(R.id.button3);
        btn4= (Button) findViewById(R.id.button4);

        tv1 = (TextView)findViewById(R.id.key1_tv);
        tv2 = (TextView)findViewById(R.id.key2_tv);
        tv3 = (TextView)findViewById(R.id.key3_tv);
        tv4 = (TextView)findViewById(R.id.key4_tv);

        HardwareControler.exportGPIOPin(73);//使能端口
        HardwareControler.setGPIODirection(73, GPIOEnum.IN);//设置GPIO为输入模式
        HardwareControler.exportGPIOPin(74);//使能端口
        HardwareControler.setGPIODirection(74, GPIOEnum.IN);//设置GPIO为输入模式
        HardwareControler.exportGPIOPin(75);//使能端口
        HardwareControler.setGPIODirection(75, GPIOEnum.IN);//设置GPIO为输入模式
        HardwareControler.exportGPIOPin(76);//使能端口
        HardwareControler.setGPIODirection(76, GPIOEnum.IN);//设置GPIO为输入模式

        btn1.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        btn2.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        btn3.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        btn4.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));

        lastkeysta=new int[4];


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x101) {
                    if (msg.getData().getInt("key0") == GPIOEnum.LOW) {
//                        GPIOtxv1.setText("GPIO 9 :  Low");
                        btn1.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
                        tv1.setText("低      ");
                    } else {
//                       GPIOtxv1.setText("GPIO 9 :  High");
                        btn1.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                        tv1.setText("高      ");
                    }
                    if(msg.getData().getInt("key1")  == GPIOEnum.LOW) {
//                        GPIOtxv2.setText("GPIO10:  Low");
                        btn2.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
                        tv2.setText("低      ");
                    } else {
//                        GPIOtxv2.setText("GPIO10:  High");
                        btn2.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                        tv2.setText("高      ");
                    }
                    if (msg.getData().getInt("key2")  == GPIOEnum.LOW) {
//                        GPIOtxv3.setText("GPIO11:  Low");
                        btn3.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
                        tv3.setText("低      ");
                    } else {
//                        GPIOtxv3.setText("GPIO11:  High");
                        btn3.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                        tv3.setText("高      ");
                    }
                    if (msg.getData().getInt("key3")  == GPIOEnum.LOW) {
//                        GPIOtxv4.setText("GPIO12:  Low");
                        btn4.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
                        tv4.setText("低      ");
                    } else {
//                        GPIOtxv4.setText("GPIO12:  High");
                        btn4.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
                        tv4.setText("高      ");
                    }
                }
                super.handleMessage(msg);
            }
        };
        t.start();
    }

    @Override
    protected void onDestroy() {
        //将线程销毁掉
        t.interrupt();
        super.onDestroy();
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted())
        {
            Message m=handler.obtainMessage();
            int keydown[];
            keydown=new int[4];
            for(int i =0;i<4;i++) {
                keydown[i] = HardwareControler.getGPIOValue(73 + i);
            }
            Bundle bundle =new Bundle();
            bundle.putInt("key0",keydown[0]);
            bundle.putInt("key1",keydown[1]);
            bundle.putInt("key2",keydown[2]);
            bundle.putInt("key3",keydown[3]);
            m.setData(bundle);
            m.what=0x101;
            if(lastkeysta[0]!=keydown[0]||lastkeysta[1]!=keydown[1]||lastkeysta[2]!=keydown[2]||lastkeysta[3]!=keydown[3])
            {
                handler.sendMessage(m);
            }
            for(int i=0;i<4;i++) {
                lastkeysta[i]=keydown[i];
            }
        }
    }

}
